// api/index.js
const host_url = "http://127.0.0.1:4040";
const get = (url, data = {}) => {
  return new Promise((resolve, reject) => {
    wx.request({
      url: `${host_url}${url}`,
      data: data,
      method: 'GET',
      success(res) {
        if (res.statusCode === 200) {
          resolve(res.data);
        } else {
          reject(new Error(`请求失败，状态码：${res.statusCode}`));
        }
      },
      fail(err) {
        reject(err);
      }
    });
  });
};

const post = (url, data = {}) => {
  return new Promise((resolve, reject) => {
    wx.request({
      url: `${host_url}${url}`,
      data: data,
      method: 'POST',
      success(res) {
        if (res.statusCode === 200) {
          resolve(res.data);
        } else {
          reject(new Error(`请求失败，状态码：${res.statusCode}`));
        }
      },
      fail(err) {
        reject(err);
      }
    });
  });
};

module.exports = {
  get,
  post,
};